﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel.Design;

namespace SharpDiary.Core.Controls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class RichUserControl : UserControl
    {
        private LinearGradientBrush _selectedItemBkBrush;
        private LinearGradientBrush _normalItemBkBrush;
        private Pen _selectedItemBorderPenOuter = new Pen(Color.FromArgb(183, 231, 252));
        private Pen _selectedItemBorderPenInner = new Pen(Color.FromArgb(223, 242, 253), 1);
        private MouseStatus _mouseStatus = MouseStatus.Normal;

        public RichUserControl()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;
            //SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

            UpdateBrushes();
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);

            e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
            e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
        }

        private void Control_MouseLeave(object sender, EventArgs e)
        {
            _mouseStatus = MouseStatus.Normal;
            Invalidate();
        }

        private void Control_MouseEnter(object sender, EventArgs e)
        {
            _mouseStatus = MouseStatus.MouseEnter;
            Invalidate();
        }

        private void UpdateBrushes()
        {
            if (this.ClientRectangle.Width == 0 || this.ClientRectangle.Height == 0) { return; }

            _selectedItemBkBrush = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(30, 30, 30), Color.FromArgb(162, 162, 162), 90);
            _normalItemBkBrush = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(30, 30, 30), Color.FromArgb(162, 162, 162), 90);

        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            UpdateBrushes();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            switch (_mouseStatus)
            {
                case MouseStatus.Normal:
                    {
                        e.Graphics.FillRectangle(_normalItemBkBrush, e.ClipRectangle);

                        break;
                    }
                case MouseStatus.MouseEnter:
                    {
                        e.Graphics.FillRectangle(_selectedItemBkBrush, e.ClipRectangle);

                        break;
                    }
            }
        }

    }
}
